package device

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/device"
	deviceReq "github.com/flipped-aurora/gin-vue-admin/server/model/device/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ModelApi struct{}

// CreateModel 创建model表
// @Tags Model
// @Summary 创建model表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body device.Model true "创建model表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /model/createModel [post]
func (modelApi *ModelApi) CreateModel(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var model device.Model
	err := c.ShouldBindJSON(&model)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = modelService.CreateModel(ctx, &model)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteModel 删除model表
// @Tags Model
// @Summary 删除model表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body device.Model true "删除model表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /model/deleteModel [delete]
func (modelApi *ModelApi) DeleteModel(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	err := modelService.DeleteModel(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteModelByIds 批量删除model表
// @Tags Model
// @Summary 批量删除model表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /model/deleteModelByIds [delete]
func (modelApi *ModelApi) DeleteModelByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := modelService.DeleteModelByIds(ctx, IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateModel 更新model表
// @Tags Model
// @Summary 更新model表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body device.Model true "更新model表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /model/updateModel [put]
func (modelApi *ModelApi) UpdateModel(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var model device.Model
	err := c.ShouldBindJSON(&model)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = modelService.UpdateModel(ctx, model)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindModel 用id查询model表
// @Tags Model
// @Summary 用id查询model表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询model表"
// @Success 200 {object} response.Response{data=device.Model,msg=string} "查询成功"
// @Router /model/findModel [get]
func (modelApi *ModelApi) FindModel(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	remodel, err := modelService.GetModel(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(remodel, c)
}

// GetModelList 分页获取model表列表
// @Tags Model
// @Summary 分页获取model表列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query deviceReq.ModelSearch true "分页获取model表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /model/getModelList [get]
func (modelApi *ModelApi) GetModelList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo deviceReq.ModelSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := modelService.GetModelInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetModelPublic 不需要鉴权的model表接口
// @Tags Model
// @Summary 不需要鉴权的model表接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /model/getModelPublic [get]
func (modelApi *ModelApi) GetModelPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	modelService.GetModelPublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的model表接口信息",
	}, "获取成功", c)
}
